package com.mhs.hotcall;

import java.util.ArrayList;
import java.util.List;

import com.mhs.BUS.BenhVienBUS;
import com.mhs.DTO.BenhVienDTO;
import com.mhs.mhs_gui.R;
import com.mhs.moreadapter.MyListViewAdapter;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class HotCall extends Activity {
	ListView myListHotCall;
	String[] arrStrNameAndFoneNumber;
	List<String> lstStrFoneNumber = new ArrayList<String>();
	String[] arrStrNameHospital;
	List<BenhVienDTO> lstBenhVien = new ArrayList<BenhVienDTO>();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hotcall);

		myListHotCall = (ListView) findViewById(R.id.lv_hotcall);

		myListHotCall.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parentView, View v,
					int position, long id) {
				String foneNumber = lstStrFoneNumber.get(position).trim();
				call(foneNumber);
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();
		setListViewAdapter();

	}

	private void setListViewAdapter() {
		BenhVienBUS bvBUS = new BenhVienBUS();
		lstBenhVien = bvBUS.getAllInfoBenhVienDTO(HotCall.this);

		if (lstBenhVien == null) {
			arrStrNameAndFoneNumber = new String[1];
			arrStrNameHospital = new String[1];
			String nameHospital = "Số khẩn cấp";
			String foneNumberHospital = "0903781043";
			String strNameAndFoneNumber = nameHospital + "\r\n" + "SĐT: "
					+ foneNumberHospital;
			arrStrNameAndFoneNumber[0] = strNameAndFoneNumber;
			arrStrNameHospital[0] = nameHospital;
			lstStrFoneNumber.add(foneNumberHospital);

			myListHotCall.setAdapter(new MyListViewAdapter(this,
					arrStrNameAndFoneNumber, R.layout.myrow));
		} else if (lstBenhVien.size() > 0 && lstBenhVien != null) {
			arrStrNameAndFoneNumber = new String[lstBenhVien.size()];
			arrStrNameHospital = new String[lstBenhVien.size()];
			for (int i = 0; i < lstBenhVien.size(); i++) {
				BenhVienDTO bv = lstBenhVien.get(i);
				String nameHospital = bv.getTENBV();
				String foneNumberHospital = bv.getDIENTHOAI();
				String strNameAndFoneNumber = nameHospital + "\r\n" + "SĐT: "
						+ foneNumberHospital;
				arrStrNameAndFoneNumber[i] = strNameAndFoneNumber;
				arrStrNameHospital[i] = nameHospital;
				lstStrFoneNumber.add(foneNumberHospital);
			}
			// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
			// android.R.layout.simple_list_item_1, arrStrNameAndFoneNumber);
			// myListHotCall.setAdapter(adapter);
			myListHotCall.setAdapter(new MyListViewAdapter(this,
					arrStrNameAndFoneNumber, R.layout.myrow));
			registerForContextMenu(myListHotCall);
		}
	}

	// 0903781043
	private void call(String phoneNumber) {
		try {
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + phoneNumber));
			startActivity(callIntent);
		} catch (ActivityNotFoundException e) {
			Log.e("MHS", "Call failed", e);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.lv_hotcall) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			menu.setHeaderIcon(R.drawable.arrow_down);
			menu.setHeaderTitle(arrStrNameHospital[info.position]);
			String[] menuItems = getResources().getStringArray(
					R.array.menuHotCallBenhVien);
			for (int i = 0; i < menuItems.length; i++) {
				menu.add(Menu.NONE, i, i, menuItems[i]);
			}
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		// int menuItemIndex = item.getItemId(); // 0
		// String[] menuItems =
		// getResources().getStringArray(R.array.menuHotCallBenhVien); //
		// String[1] -> thong tin benh vien
		// String menuItemName = menuItems[menuItemIndex]; // thong tin benh
		// vien
		// String listItemName = arrStrNameHospital[info.position]; // benh vien
		// thong nhat
		Intent tt = new Intent(HotCall.this, TraCuuBenhVien_Search_Detail.class);
		int maBV = lstBenhVien.get(info.position).getMABV_CLIENT();
		tt.putExtra("maBV", maBV);
		startActivity(tt);
		return true;
	}
}
